package day04.exercise;

import java.util.Scanner;

public class LoopDemo5 {
    public static void main(String[] args) {
        //编写一个Java程序，使用while循环计算一个整数的位数并打印
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入数字：");
        int num = scanner.nextInt();
        int count = 1;
        System.out.print(num);
        while (num / 10 != 0) {
            num /= 10;
            count++;
        }
        System.out.println("的位数为：" + count + "位");

        //编写一个Java程序，使用for循环和莱布尼茨级数公式计算圆周率π的近似值。莱布尼茨级数公式：
        //                                  π/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
        double pi = 0;
        int count1 = 0;
        for (int i = 1; i <= 1E9+1; i++) {

            if(i%2==0){
                continue;
            }
            if (count1 % 2 == 0) {
                pi = pi + 1.0 / i;
                count1++;
            }
            else {
                pi = pi - 1.0/i;
                count1++;
            }
        }
        System.out.println(pi*4);
    }
}
